<template>
<div>
  <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :option="agentOption"
               @search-change="searchChange"
               @selection-change="selectionChange"
               @on-load="getDataList">

      <template slot-scope="scope"
                slot="agentAddress">
                {{scope.row.province+scope.row.city+scope.row.agentAddress}}
      </template>

      <template slot="menuLeft">
        <el-button type="primary"
        size="small"
        plain
        @click.stop="addOrUpdateHandle()">新增</el-button>
      </template>

      <template slot-scope="scope"
                slot="menu">
        <el-button type="sucess"
                   size="small"
                   @click.stop="addOrUpdateHandle(scope.row)">详情/编辑</el-button>

        <el-button type="danger"
                   icon="el-icon-delete"
                   size="small"
                   @click.stop="deleteHandle(scope.row.agentId)">删除</el-button>

      </template>
    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-bind:shopInfo="shopInfo"
                v-if="addOrUpdateVisible"
                ref="addOrUpdate"
                @refreshDataList="getDataList"></add-or-update>

</div>
</template>

<script>
import { agentOption } from '@/crud/agent/agent.js'
import AddOrUpdate from './agent-add-or-update'
export default {
    data() {
      return {
        dataList: [],
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        agentOption: agentOption,
        addOrUpdateVisible :false,
        shopInfo:'',
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
      }
    },
    components:{
      AddOrUpdate
    },
    methods: {
      getDataList (page, params) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/agent/agent/page'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign({
              current:page == null? this.page.currentPage : page.currentPage,
              size:page == null? this.page.pageSize : page.pageSize,
            },
            params
            )
          )
        }).then(({ data }) => {
            this.dataList = data.records
            this.page.total = data.total
            this.dataListLoading = false
        }).catch((err) =>{
        })
      },

      searchChange (params) {
        this.getDataList(this.page, params)
    },

      selectionChange (val) {
        this.dataListSelections = val
    },
      // 删除
    deleteHandle (id) {
      console.log(id)
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.userId
      })
      this.$confirm(`确定进行删除代理商?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/agent/agent'),
            method: 'delete',
            data: this.$http.adornData(ids, false)
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
          // alert('该模块未完成')
        })
        .catch(() => { })
    },
    // 新增 / 修改
      addOrUpdateHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row)
        })
        this.getDataList()
      },

      //更改店铺审核状态为 审核通过
      changeShopState(id){
        this.$http({
          url: this.$http.adornUrl("/shop/shopDetail/shopStatus?shopId="+id+"&shopStatus="+1),
          method: 'put',
          }).then((res) => {
            console.log(res);
            this.getDataList()
          })
      }
    },

}
</script>
